package com.ygo.feihua.fragment;
import android.support.v4.app.*;
import android.view.*;
import android.os.*;
import android.support.design.widget.*;
import android.support.v4.view.*;
import java.util.*;
import android.support.v4.util.*;
import android.widget.*;
import android.util.Log;
import com.ygo.feihua.BmobTable.*;
import com.ashokvarma.bottomnavigation.*;
import android.graphics.*;
import com.ygo.feihua.R;
import com.ygo.feihua.*;
import com.xiaomi.mistatistic.sdk.*;

public class FragmentMain extends Fragment
{

	private TabLayout main_tab;
	private ViewPager main_pag;
	private List<Map<String,Object>> fg=new ArrayList<Map<String,Object>>();
	private FragmentMain.Find_tab_Adapter fAdapter;

	private GroupChatFragment fmtZixun;
	private FragmentShequ fmtShequ;
	private FragmentZhuye fmtZhuye;
	
	private CeHua ch;

	
	public TieType getShequType()
	{
		return fmtShequ.getShequType();
		// TODO: Implement this method
	}

	public void shequSettingLongClick()
	{
		fmtShequ.longTieType();
		// TODO: Implement this method
	}

	public void shequSettingClick()
	{
		fmtShequ.tie_type("帖子类型",null);
		//fmtShequ.longTieType();
		// TODO: Implement this method
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View v=inflater.inflate(R.layout.fragment_main,null);
		main_tab=((CeHua)getActivity()).main_tab;
		main_pag=(ViewPager) v.findViewById(R.id.main_pag);
		
		ch=(CeHua) getActivity();
		
		fmtZhuye=new FragmentZhuye();
		Map<String,Object> map1=new ArrayMap<String,Object>();
		map1.put("fr",fmtZhuye);
		map1.put("title","主页");
		fg.add(map1);	
		
		fmtShequ=new FragmentShequ();
		Map<String,Object> map2=new ArrayMap<String,Object>();
		map2.put("fr",fmtShequ);
		map2.put("title","社区");
		fg.add(map2);	
		
		fmtZixun=new GroupChatFragment();
		Map<String,Object> map3=new ArrayMap<String,Object>();
		map3.put("fr",fmtZixun);
		map3.put("title","群聊");
		fg.add(map3);	
		
		
		
		//设置TabLayout的模式
        main_tab.setTabMode(TabLayout.MODE_FIXED);
        fAdapter = new Find_tab_Adapter(getActivity().getSupportFragmentManager());

        //viewpager加载adapter
        main_pag.setAdapter(fAdapter);
		//缓存两个页面
		main_pag.setOffscreenPageLimit(3);
        //TabLayout加载viewpager
		main_tab.setupWithViewPager(main_pag);
		
		main_pag.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){

				@Override
				public void onPageScrolled(int p1, float p2, int p3)
				{
					// TODO: Implement this method
				}

				@Override
				public void onPageSelected(int position)
				{
					Fragment fmt=(Fragment)fg.get(position).get("fr");
					if(fmt.equals(fmtShequ)){
						ch.setToolShequ();
					}else{
						ch.setToolMain();
					}
					// TODO: Implement this method
				}

				@Override
				public void onPageScrollStateChanged(int position)
				{
					
					// TODO: Implement this method
				}
			});
		
		
			
		// TODO: Implement this method
		return v;
	}

	@Override
	public void onResume(){
		// TODO: Implement this method
		super.onResume();
		MiStatInterface.recordPageStart(getActivity(), "主页");
	}

	@Override
	public void onPause(){
		// TODO: Implement this method
		super.onPause();
		MiStatInterface.recordPageEnd();
	}
	
	class Find_tab_Adapter extends FragmentPagerAdapter {
		public Find_tab_Adapter(FragmentManager fm) {
			super(fm);
		}


		@Override
		public Fragment getItem(int position) {
			//Log.e("FragmentMain","调用Item");
			
			return (Fragment)fg.get(position).get("fr");
		}

		@Override
		public int getCount() {
			return fg.size();
		}

		//此方法用来显示tab上的名字
		@Override
		public CharSequence getPageTitle(int position) {
			//Log.e("FragmentMain","调用Title");
			return (String)fg.get(position).get("title");
		}
	}

	
	
}
